package config

import (
	"os"

	"gopkg.in/yaml.v2"
)

type SysCfg struct {
	RmsTaskOrder    bool       `yaml:"rms_task_order"`     //强制rms任务顺序
	UseAllPath      bool       `yaml:"use_all_path"`       //空闲避让是否选取所有路径
	GlobalExtendNum int        `yaml:"global_extend_num"`  //全局路径上寻找避让点的膨胀次数
	MinPathLimitNum int        `yaml:"min_path_limit_num"` //出工位最小数量
	HttpPort        string     `yaml:"http_port"`          //http监听端口
	MongoCfg        MongoCfg   `yaml:"mongo_cfg"`          //mongodb参数
	TaskCfg         TaskConfig `yaml:"task_cfg"`           //任务参数
	Grpc            struct {
		MaxSize int `yaml:"max_size"`
	}
	PathNumApplyDoor         int         `yaml:"path_num_apply_door"`         //还剩多少路径申请自动门等设备
	PathNumApplyConveyorLine int         `yaml:"path_num_apply_conveyorline"` //还剩下多少路径开始请求输送线路
	AuthorModel              AuthorModel `yaml:"author_model"`                //授权模块
	UseHeadCatch             bool        `yaml:"use_head_catch"`              //是否使用头部缓冲计算局部路径
	CallTimeout              int         `yaml:"call_timeout"`                //请求设备超时时间
}

type TaskConfig struct {
	MatchMultiple int64 `yaml:"match_task_multiple"`
}
type AuthorModel struct {
	TcpServerAddr string `yaml:"tcp_server_addr"`
	MaxHour       int    `yaml:"max_hour"`
	Switch        int    `yaml:"switch"`
}

var Conf *SysCfg

func init() {
	Conf = &SysCfg{}
	f, err := os.Open("rms.yaml")
	if err != nil {
		panic("can not read rms.yaml")
	}
	if errDecode := yaml.NewDecoder(f).Decode(Conf); errDecode != nil {
		panic("decode yaml file error:" + errDecode.Error())
	}
	if Conf.TaskCfg.MatchMultiple == 0 {
		Conf.TaskCfg.MatchMultiple = 5 //默认5
	}
	if Conf.MinPathLimitNum < 2 {
		Conf.MinPathLimitNum = 1
	}
	if Conf.AuthorModel.MaxHour < 2 {
		Conf.AuthorModel.MaxHour = 2
	}
	if Conf.AuthorModel.Switch != 0 && Conf.AuthorModel.Switch != 1 {
		Conf.AuthorModel.Switch = 0
	}
	if Conf.PathNumApplyDoor < 1 {
		Conf.PathNumApplyDoor = 1
	}
	if Conf.PathNumApplyConveyorLine < 1 {
		Conf.PathNumApplyConveyorLine = 1
	}
	if Conf.CallTimeout < 5 {
		Conf.CallTimeout = 5
	}
}
